package cn.gailvlun.gll.presentation.xroom.vm

import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.net.base.Profile
import cn.gailvlun.gll.net.cross.CrossRoomInfo
import cn.gailvlun.gll.net.cross.StartMatchRes
import cn.gailvlun.gll.util.ProfileUtil
import io.realm.Realm

class CrossRoomGameInfoViewModel: ViewModel {

    var startMatchRes: StartMatchRes? = null
    var crossGameInfo: CrossRoomInfo? = null

    var realm: Realm? = null

    constructor() : super() {
        realm = Realm.getInstance(ChatDB2.getConfiguration())
    }

    override fun onCleared() {
        realm?.close()

        super.onCleared()
    }

    fun getCrossRoomInfo(id: Int) {
        crossGameInfo = realm?.where(CrossRoomInfo::class.java)
                ?.equalTo("id", id)
                ?.findFirst()
    }

    fun getOppositeProfile(): Profile? {
        return if (crossGameInfo?.first?.owner?.id == ProfileUtil.getUserId()) {
            crossGameInfo?.second?.owner
        } else {
            crossGameInfo?.first?.owner
        }
    }
}